package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Address;
import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.UserDetail;
import cn.piaoyi.bean.UserInfo;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.PosterService;
import cn.piaoyi.service.UserInfoService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.interceptor.RemoteAddrAware;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class Register extends BaseAction implements SessionAware, RemoteAddrAware{
	private static final Log logger = LogFactory.getLog(Register.class);
	
	private UserInfoService userInfoService;
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	private String remoteAddr;
	private Map session;
	
	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	
	private String account;
	private String password;
	private String passfirm;
	private String email;
	private String mobile;

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassfirm() {
		return passfirm;
	}

	public void setPassfirm(String passfirm) {
		this.passfirm = passfirm;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		this.setAction("add");
		return SUCCESS;
	}
	
	/**
	 * 添加
	 */
	public String add(){
		UserInfo ui = this.getUserInfoService().findUserInfoByAccount(this.account);
		if(ui != null){
			this.addFieldError("error", this.getText("error.reg.user"));
	        return SUCCESS;
		}
		ui = new UserInfo();
		ui.setAccount(this.account);
		ui.setPassword(Util.hash(this.password));
		ui.setMobile(this.mobile);
		ui.setEmail(this.email);
		ui.setRegTime(new Date());
		ui.setLastLoginTime(new Date());
		ui.setLastLoginIP(this.getRemoteAddr());
		ui.setValidated(1);
		
		UserDetail userDetail = new UserDetail();
		userDetail.setNotenum(0);
		userDetail.setMobile("");
		userDetail.setTel("");
		userDetail.setQq("");
		userDetail.setEmail("");
		userDetail.setSex(0);
		
		Address address = new Address();
		address.setConsignee("");
		address.setProvince(0);
		address.setCity(0);
		address.setDistrict("");
		address.setAddress("");
		address.setZipcode("");
		address.setMobile("");
		address.setEmail("");
		try {
			this.getUserInfoService().createUserInfo(ui,address);
		} catch (PiaoyiException e) {
            this.addActionError(this.getText("error.reg.createrror"));
            return ERROR;
        }
		return "login";
	}

}
